package com.kpmg.datalake.common.utils;

import java.util.Random;

/**
 * 生成随机字符
 *
 * @author Alex.zhao [alex.zhao@kpmg.com]
 * @since Created on azhao6 on 2018-08-21 16:17:17 随机字符生成
 */
public class RandStrUtil {

  /**
   * 72个字符集
   */
  private static char[] chars = {'0', '1', '2', '3', '4', '5', '6', '7',
      '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
      'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
      'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
      'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
      'Z', '!', '@', '#', '$', '%', '^', '&', '*', '~', '|'};

  private RandStrUtil() {
  }

  public static String getComplexStr(int passLength) {
    return getRandStr(RandType.COMPLEX, passLength);
  }

  /**
   * @param passLength 随机密码长度
   * @return 随机密码数组
   */
  public static String getRandStr(RandType randType, int passLength) {
    StringBuilder password = new StringBuilder();
    Random random = new Random();
    switch (randType) {
      case NUMBER:
        for (int m = 1; m <= passLength; m++) {
          password.append(chars[random.nextInt(10)]);
        }

        break;
      case CHARACTER:
        for (int m = 1; m <= passLength; m++) {
          password.append(chars[random.nextInt(53) + 10]);
        }
        break;
      case COMPLEX:
        for (int m = 1; m <= passLength; m++) {
          password.append(chars[random.nextInt(72)]);
        }

        break;
      default:
    }
    return password.toString();
  }

  public enum RandType {
    /**
     * 数字
     */
    NUMBER,
    /**
     * 字符
     */
    CHARACTER,
    /**
     * 复合
     */
    COMPLEX
  }
}
